﻿using Abp.Domain.Repositories;
using Batman.Cms.Core.Services;
using Batman.Cms.Core.Tools;
using Batman.Cms.Domains;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Batman.Cms.Services.Words
{
    public class StopWordListAppSvc : ServiceBase<StopWordList>
    {
        public StopWordListAppSvc(IRepository<StopWordList> repository, ToolBox toolBox) : base(repository, toolBox)
        {
        }

        public void ImportStopWordList(IFormFile file, string belong)
        {
            var stream = file.OpenReadStream();

            var records = new List<StopWordList>();

            using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
            {
                var word = string.Empty;
                var query = Repository.GetAll().AsNoTracking();

                while (!string.IsNullOrEmpty((word = sr.ReadLine())))
                {
                    var isExists = query.Any(t => t.Word == word);
                    if (isExists) continue;

                    records.Add(new StopWordList(word, belong));
                }
            }

            records.ForEach(t => Repository.Insert(t));

        }
    }
}
